<?php
class Controller_Admin_Popularize extends Controller_Form
{
    public function __construct ($req, $res)
    {
        $this->_form_type = 'popu_result';
        $this->_no_buttons= TRUE;
        parent::__construct($req, $res);

        $this->_list_pagesize = 20;
        $this->_list_where = array(
        );

        $this->_shopId = App_Shop_Manager::currentShopId();
        if ($this->_shopId > 0)
            $this->_list_where[] = array('shopId', '=', $this->_shopId);
    }

    public function action_index ()
    {
        $this->action_list();
    }

    public function action_list ($page = 1)
    {
        $filter = array();
        $query  = $this->request->query();

        $guider = new Guider;
        $filter['startDay'] = Arr::get($query, 'startDay');
        $filter['stopDay']  = Arr::get($query, 'stopDay');
        $filter['status']   = Arr::get($query, 'status', -1);
        $filter['shopId']   = Arr::get($query, 'shopId');
        $filter['taskType'] = Arr::get($query, 'taskType');

        $filter['statusList'] = Popularize::optionsStatusList();
        $filter['shopList']   = $guider->optionsAllGuiders();

        if ($this->_shopId > 0)
            $filter['shopId'] = $this->_shopId;

        if (empty($filter['startDay']))
            $filter['startDay'] = date("Y-m-d", time() - 86400 * 7);

        if (empty($filter['stopDay']))
            $filter['stopDay'] = date("Y-m-d", time());

        // list_where
        if ($filter['status'] != -1 && ! empty($filter['status']))
            $this->_list_where[] = array('a.status', '=', $filter['status']);

        if ($filter['shopId'] > 0)
            $this->_list_where[] = array('a.uid', '=', $filter['shopId']);

        if (! empty($filter['taskType']))
            $this->_list_where[] = array('a.taskType', '=', $filter['taskType']);

        if (! empty($filter['startDay']))
            $this->_list_where[] = array('a.created', '>=', strtotime($filter['startDay']));

        if (! empty($filter['stopDay']))
            $this->_list_where[] = array('a.created', '<', strtotime($filter['stopDay']) + 86400);

        $this->template->set('form_include_head_file', $this->_form->include_edit_file());
        $this->template->set('filter', $filter);

        parent::action_list($page);
    }



    public function action_create ()
    {
        $post = $this->request->post();
        if (! empty($post))
        {
            $this->rebuild_post_data($post);
            $this->_form_data = $post;
        }

        parent::action_create();
    }

    public function action_update ($nid)
    {
        $post = $this->request->post();
        if (! empty($post))
        {
            $this->rebuild_post_data($post);
            $this->_form_data = $post;
        }
        else
        {
            $data = Form_Data::detail($this->_form_type, $nid); 
            $this->_form_data = $data;
        }

        parent::action_update($nid);
    }

    public function action_detail ($nid)
    {
        $popu   = new Popularize($nid);
        $detail = $popu->detail();

        // 商家账户
        $guiderUid = Arr::get($detail, 'uid');
        $guider    = new Guider($guiderUid);
        $detail['guider'] = $guider->getUserInfo();

        $bank   = new UserExt_Bank($guiderUid);
        $banks  = $bank->getBanKList();

        $this->template->set('title', '结算详细');
        $this->template->set('detail', $detail);
        $this->template->set('banks', $banks);
        $this->template->set_filename("smarty:www/popularize/admin_detail");
    }

    public function action_setStatus ($nid)
    {
        $post     = $this->request->post();
        $status   = Arr::get($post, 'status');
        $comments = Arr::get($post, 'comments');

        $order = new Popularize($nid);
        $order->setStatus($status, $comments);

        echo json_encode(array('status' => 0));exit;
    }

    private function rebuild_post_data (& $post)
    {
        //
        $post['managerUid'] = user::current_user_id();
        // 起始时间
    }

    public function list_get_data ($offset, $limit)
    {
        $popu = new Popularize;
        list($count, $list) = $popu->search($this->_list_where, $offset, $limit);
        $this->_list_total_size = $count;
        return $list;
    }

    public function list_filter_data (& $rows)
    {
        foreach ($rows as & $rs)
        {
            //
            $rs['money'] = '€'.number_format($rs['money'], 2);
            
        }
    }

    public function classname_by_list_row (& $row)
    {
    }

    public function actions_by_list_row (& $row, & $menus)
    {
	    $controller = Request::initial()->controller();
	    $directory  = Request::initial()->directory();
	
	    $directory  = empty($directory) ? '' : '/'.$directory;

        unset($menus[1]);
        unset($menus[2]);

        $status = Arr::get($row, 'status');
        if ($status != '已完成')
        {
            $menus[] = "<a href=\"javascript:;\" onClick=\"handle_prize_bill('{$row['id']}')\">处理</a>";
        }
        else
        {
        }
    }
}


